<?php

require_once 'util.php';

class Feedback {

    //this singleton
    private static $instance;
    //results of Davide's function
    protected $results;
    //stuff to access the api
    private $facebook;
    private $user;
    private $user_details;

    private function __construct($res) {
        $this->results = $res;
        $this->facebook = fb_authenticate();
        $this->user = $this->facebook->getUser();
        $this->user_details = get_user_details($this->user, $this->facebook, "friends", $this->facebook->getAccessToken());
    }

    public static function getInstance($res) {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c($res);
        }

        return self::$instance;
    }

    public function getAllResults() {
        return $this->results;
    }

    public function buildRandomResults($n) {

        if ($n < 1) {
            $n = 1;
        }

        $friends = get_friends_name_and_id($this->user, $this->facebook, $this->user_details);

        $rand_ids = array_rand($friends, $n);

        $fakeResults = array(array());

        foreach ($rand_ids as $id) {
            $name_score = array("name" => $friends[$id], "score" => rand(1, 5));
            $fakeResults[$id] = $name_score;
        }

        unset($fakeResults[0]);

        $this->results = $fakeResults;

        return $this->getAllResults();
    }

}

?>
